import 'package:flutter/material.dart';

// 设备类型枚举
enum DeviceType {
  mobile,
  tablet,
  desktop,
  largeDesktop,
}

// 屏幕尺寸类别枚举
enum ScreenSize {
  xs, // < 600px
  sm, // 600px - 1024px
  md, // 1024px - 1440px
  lg, // 1440px - 1920px
  xl, // > 1920px
}

/// UI组件库响应式设计工具
/// 提供屏幕尺寸判断和响应式布局支持
class UIResponsive {
  // 断点定义
  static const double mobileBreakpoint = 600;
  static const double tabletBreakpoint = 1024;
  static const double desktopBreakpoint = 1440;
  static const double largeDesktopBreakpoint = 1920;

  // 获取设备类型
  static DeviceType getDeviceType(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    
    if (screenWidth >= largeDesktopBreakpoint) {
      return DeviceType.largeDesktop;
    } else if (screenWidth >= desktopBreakpoint) {
      return DeviceType.desktop;
    } else if (screenWidth >= mobileBreakpoint) {
      return DeviceType.tablet;
    } else {
      return DeviceType.mobile;
    }
  }

  // 获取屏幕尺寸类别
  static ScreenSize getScreenSize(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    
    if (screenWidth >= largeDesktopBreakpoint) {
      return ScreenSize.xl;
    } else if (screenWidth >= desktopBreakpoint) {
      return ScreenSize.lg;
    } else if (screenWidth >= tabletBreakpoint) {
      return ScreenSize.md;
    } else if (screenWidth >= mobileBreakpoint) {
      return ScreenSize.sm;
    } else {
      return ScreenSize.xs;
    }
  }

  // 设备类型判断
  static bool isMobile(BuildContext context) {
    return getDeviceType(context) == DeviceType.mobile;
  }

  static bool isTablet(BuildContext context) {
    return getDeviceType(context) == DeviceType.tablet;
  }

  static bool isDesktop(BuildContext context) {
    final deviceType = getDeviceType(context);
    return deviceType == DeviceType.desktop || deviceType == DeviceType.largeDesktop;
  }

  static bool isLargeDesktop(BuildContext context) {
    return getDeviceType(context) == DeviceType.largeDesktop;
  }

  // 屏幕尺寸判断
  static bool isXS(BuildContext context) {
    return getScreenSize(context) == ScreenSize.xs;
  }

  static bool isSM(BuildContext context) {
    return getScreenSize(context) == ScreenSize.sm;
  }

  static bool isMD(BuildContext context) {
    return getScreenSize(context) == ScreenSize.md;
  }

  static bool isLG(BuildContext context) {
    return getScreenSize(context) == ScreenSize.lg;
  }

  static bool isXL(BuildContext context) {
    return getScreenSize(context) == ScreenSize.xl;
  }

  // 响应式数值计算
  static double responsiveValue(
    BuildContext context, {
    required double mobile,
    double? tablet,
    double? desktop,
    double? largeDesktop,
  }) {
    final deviceType = getDeviceType(context);
    
    switch (deviceType) {
      case DeviceType.mobile:
        return mobile;
      case DeviceType.tablet:
        return tablet ?? mobile * 1.2;
      case DeviceType.desktop:
        return desktop ?? tablet ?? mobile * 1.4;
      case DeviceType.largeDesktop:
        return largeDesktop ?? desktop ?? tablet ?? mobile * 1.6;
    }
  }

  // 响应式字体大小
  static double responsiveFontSize(
    BuildContext context, {
    required double baseFontSize,
  }) {
    final deviceType = getDeviceType(context);
    
    switch (deviceType) {
      case DeviceType.mobile:
        return baseFontSize * 0.9;
      case DeviceType.tablet:
        return baseFontSize;
      case DeviceType.desktop:
        return baseFontSize * 1.1;
      case DeviceType.largeDesktop:
        return baseFontSize * 1.2;
    }
  }

  // 响应式间距
  static double responsiveSpacing(
    BuildContext context, {
    required double baseSpacing,
  }) {
    final deviceType = getDeviceType(context);
    
    switch (deviceType) {
      case DeviceType.mobile:
        return baseSpacing * 0.8;
      case DeviceType.tablet:
        return baseSpacing;
      case DeviceType.desktop:
        return baseSpacing * 1.2;
      case DeviceType.largeDesktop:
        return baseSpacing * 1.4;
    }
  }

  // 网格列数计算
  static int getGridColumns(
    BuildContext context, {
    int mobileColumns = 1,
    int tabletColumns = 2,
    int desktopColumns = 3,
    int largeDesktopColumns = 4,
  }) {
    final deviceType = getDeviceType(context);
    
    switch (deviceType) {
      case DeviceType.mobile:
        return mobileColumns;
      case DeviceType.tablet:
        return tabletColumns;
      case DeviceType.desktop:
        return desktopColumns;
      case DeviceType.largeDesktop:
        return largeDesktopColumns;
    }
  }

  // 侧边栏宽度计算
  static double getSidebarWidth(BuildContext context) {
    final deviceType = getDeviceType(context);
    
    switch (deviceType) {
      case DeviceType.mobile:
        return MediaQuery.of(context).size.width * 0.85; // 85% of screen
      case DeviceType.tablet:
        return 320.0;
      case DeviceType.desktop:
        return 360.0;
      case DeviceType.largeDesktop:
        return 400.0;
    }
  }

  // 内容区域最大宽度
  static double getContentMaxWidth(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    
    if (screenWidth >= largeDesktopBreakpoint) {
      return 1200.0;
    } else if (screenWidth >= desktopBreakpoint) {
      return 960.0;
    } else if (screenWidth >= tabletBreakpoint) {
      return 720.0;
    } else {
      return screenWidth - 32.0; // 16px margin on each side
    }
  }

  // 响应式边距
  static EdgeInsets responsivePadding(
    BuildContext context, {
    double mobilePadding = 16.0,
    double? tabletPadding,
    double? desktopPadding,
    double? largeDesktopPadding,
  }) {
    final padding = responsiveValue(
      context,
      mobile: mobilePadding,
      tablet: tabletPadding,
      desktop: desktopPadding,
      largeDesktop: largeDesktopPadding,
    );
    
    return EdgeInsets.all(padding);
  }

  // 响应式水平边距
  static EdgeInsets responsiveHorizontalPadding(
    BuildContext context, {
    double mobilePadding = 16.0,
    double? tabletPadding,
    double? desktopPadding,
    double? largeDesktopPadding,
  }) {
    final padding = responsiveValue(
      context,
      mobile: mobilePadding,
      tablet: tabletPadding,
      desktop: desktopPadding,
      largeDesktop: largeDesktopPadding,
    );
    
    return EdgeInsets.symmetric(horizontal: padding);
  }

  // 响应式垂直边距
  static EdgeInsets responsiveVerticalPadding(
    BuildContext context, {
    double mobilePadding = 16.0,
    double? tabletPadding,
    double? desktopPadding,
    double? largeDesktopPadding,
  }) {
    final padding = responsiveValue(
      context,
      mobile: mobilePadding,
      tablet: tabletPadding,
      desktop: desktopPadding,
      largeDesktop: largeDesktopPadding,
    );
    
    return EdgeInsets.symmetric(vertical: padding);
  }

  // 条件组件渲染
  static Widget? conditionalWidget({
    required BuildContext context,
    Widget? mobile,
    Widget? tablet,
    Widget? desktop,
    Widget? largeDesktop,
  }) {
    final deviceType = getDeviceType(context);
    
    switch (deviceType) {
      case DeviceType.mobile:
        return mobile;
      case DeviceType.tablet:
        return tablet ?? mobile;
      case DeviceType.desktop:
        return desktop ?? tablet ?? mobile;
      case DeviceType.largeDesktop:
        return largeDesktop ?? desktop ?? tablet ?? mobile;
    }
  }

  // 响应式布局包装器
  static Widget responsiveLayout({
    required BuildContext context,
    Widget? mobile,
    Widget? tablet,
    Widget? desktop,
    Widget? largeDesktop,
  }) {
    final widget = conditionalWidget(
      context: context,
      mobile: mobile,
      tablet: tablet,
      desktop: desktop,
      largeDesktop: largeDesktop,
    );
    
    return widget ?? const SizedBox.shrink();
  }

  // 获取导航类型（底部导航 vs 侧边栏导航）
  static NavigationType getNavigationType(BuildContext context) {
    return isMobile(context) 
        ? NavigationType.bottom 
        : NavigationType.sidebar;
  }

  // 获取对话框类型（全屏 vs 居中）
  static DialogType getDialogType(BuildContext context) {
    return isMobile(context) 
        ? DialogType.fullscreen 
        : DialogType.center;
  }

  // 响应式断点检查
  static bool isAboveBreakpoint(BuildContext context, double breakpoint) {
    return MediaQuery.of(context).size.width > breakpoint;
  }

  static bool isBelowBreakpoint(BuildContext context, double breakpoint) {
    return MediaQuery.of(context).size.width < breakpoint;
  }

  static bool isBetweenBreakpoints(
    BuildContext context, 
    double minBreakpoint, 
    double maxBreakpoint,
  ) {
    final screenWidth = MediaQuery.of(context).size.width;
    return screenWidth >= minBreakpoint && screenWidth < maxBreakpoint;
  }

  // 屏幕信息获取
  static Size getScreenDimensions(BuildContext context) {
    return MediaQuery.of(context).size;
  }

  static double getScreenWidth(BuildContext context) {
    return MediaQuery.of(context).size.width;
  }

  static double getScreenHeight(BuildContext context) {
    return MediaQuery.of(context).size.height;
  }

  static Orientation getOrientation(BuildContext context) {
    return MediaQuery.of(context).orientation;
  }

  static bool isLandscape(BuildContext context) {
    return getOrientation(context) == Orientation.landscape;
  }

  static bool isPortrait(BuildContext context) {
    return getOrientation(context) == Orientation.portrait;
  }

  // 安全区域信息
  static EdgeInsets getSafeAreaPadding(BuildContext context) {
    return MediaQuery.of(context).padding;
  }

  static double getStatusBarHeight(BuildContext context) {
    return MediaQuery.of(context).padding.top;
  }

  static double getBottomSafeArea(BuildContext context) {
    return MediaQuery.of(context).padding.bottom;
  }

  // 文本缩放因子
  static double getTextScaleFactor(BuildContext context) {
    return MediaQuery.of(context).textScaleFactor;
  }

  // 像素密度
  static double getDevicePixelRatio(BuildContext context) {
    return MediaQuery.of(context).devicePixelRatio;
  }
}

/// 导航类型枚举
enum NavigationType {
  bottom,
  sidebar,
}

/// 对话框类型枚举
enum DialogType {
  center,
  fullscreen,
}

/// 响应式构建器 Widget
class ResponsiveBuilder extends StatelessWidget {
  final Widget Function(BuildContext context, DeviceType deviceType) builder;

  const ResponsiveBuilder({
    Key? key,
    required this.builder,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return builder(context, UIResponsive.getDeviceType(context));
  }
}

/// 屏幕尺寸构建器 Widget
class ScreenSizeBuilder extends StatelessWidget {
  final Widget Function(BuildContext context, ScreenSize screenSize) builder;

  const ScreenSizeBuilder({
    Key? key,
    required this.builder,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return builder(context, UIResponsive.getScreenSize(context));
  }
}